/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import beans.SeekerCustomer;
import beans.SeekerUploadImg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *
 * @author Dhruv
 */

//view a mutual friend's image
public class ViewOthersImageAction extends Action {

    //constructor
    public ViewOthersImageAction(SeekerCustomer model) {
        super(model);
    }

    //identifies the operation name
    public String getName() {
        return "ViewOthersImage";
    }

    public Object perform(HttpServletRequest req, HttpServletResponse resp) {

        //JSP page to dispatch the request to
        String address = "";
        //use the session object to get the model object
        HttpSession session = req.getSession();
        model = (SeekerCustomer)session.getAttribute("session1");

        //if image viewer and image uploader are mutual friends
        //only can you view the image for tagging
        if(model.isMutualFriend(req.getParameter("uploader"),model.getName()))
        {
            address = "ViewOthersImage.jsp";
            //what image is being viewed
            model.setViewingImage(req.getParameter("uploader"), req.getParameter("uploadedImg"), req.getParameter("uploaderDT"));
            //get all tags associated with an current viewing image
            ArrayList<String> associatedTags = model.getAssociatedTags();

            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("associatedTags", associatedTags);
            req.setAttribute("viewImage", new SeekerUploadImg(req.getParameter("uploader"),
                    req.getParameter("uploadedImg"),req.getParameter("uploaderDT")));
        }
        //you cannot view the image for tagging since you are not mutual friends with uploader
        else
        {
            address = "Error.jsp";            
            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("error", "You cannot tag this image since you are not uploader's friend.");
        }

        //below object receives requests from the client and sends them to JSP address
        RequestDispatcher dispatcher = req.getRequestDispatcher(address);
        try {
            dispatcher.forward(req, resp);
        } catch (ServletException ex) {
            Logger.getLogger(CreateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
              Logger.getLogger(CreateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        //returning the object conforms to the Abstract Action class definition
        //object needed to maintain session between requests
        return model;
    }
}